package gui;

import interfaces.IServidorCliente;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.Component;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;


public class TelaCliente extends JFrame {

	private static final long serialVersionUID = -3502124746282030634L;

	private JPanel contentPane;
	private JTextField textLogin;
	private JPasswordField passwordSenha;
	private JTextField textTotal;
	private JTextField textNumero;
	private JPasswordField passwordSenhaCartao;
	private JTable table;
	private IServidorCliente rmi = null;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TelaCliente frame = new TelaCliente();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws NotBoundException 
	 * @throws RemoteException 
	 * @throws MalformedURLException 
	 */
	public TelaCliente() throws MalformedURLException, RemoteException, NotBoundException {
		// RMI
		String objname = "rmi://localhost/servidorcentral";
		System.out.println("Looking for object "+objname);
		rmi = (IServidorCliente) Naming.lookup(objname);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 437, 665);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		
		JLabel lblLogin = new JLabel("Login");
		
		textLogin = new JTextField();
		textLogin.setColumns(10);
		
		JLabel lblSenha = new JLabel("Senha");
		
		passwordSenha = new JPasswordField();
		
		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					rmi.logar(textLogin.getText(), passwordSenha.getText());
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		});
		
		textNumero = new JTextField();
		textNumero.setEnabled(false);
		textNumero.setColumns(10);

		passwordSenhaCartao = new JPasswordField();
		passwordSenhaCartao.setEnabled(false);

		JButton btnExtrato = new JButton("Extrato");
		btnExtrato.setEnabled(false);
		btnExtrato.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					rmi.consultarExtrato(textNumero.getText(), passwordSenhaCartao.getText());
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		});
		
		JLabel lblTotal = new JLabel("Total");
		
		textTotal = new JTextField();
		textTotal.setEditable(false);
		textTotal.setColumns(10);
		
		JButton btnNewButton = new JButton("Efetuar Pagamento");
		btnNewButton.setEnabled(false);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					rmi.efetuarPagamento(textNumero.getText());
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		});
		
		JLabel lblNumero = new JLabel("N\u00FAmero");
		
		
		JLabel lblSenhaCarto = new JLabel("Senha Cart\u00E3o");
		
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(17)
					.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblLogin)
						.addComponent(lblSenha)
						.addComponent(lblNumero)
						.addComponent(lblSenhaCarto))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(textLogin, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(btnLogin))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(passwordSenha, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(btnExtrato))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(textNumero, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(12)
							.addComponent(btnNewButton))
						.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
							.addComponent(passwordSenhaCartao)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblTotal)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(textTotal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(25, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblLogin)
						.addComponent(textLogin, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnLogin))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
						.addComponent(btnExtrato)
						.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
							.addComponent(passwordSenha, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(lblSenha)))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(lblNumero)
						.addComponent(textNumero, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(textTotal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblTotal)
						.addComponent(lblSenhaCarto)
						.addComponent(passwordSenhaCartao, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(14, Short.MAX_VALUE))
		);
		gl_panel.linkSize(SwingConstants.HORIZONTAL, new Component[] {passwordSenha, textLogin});
		gl_panel.linkSize(SwingConstants.HORIZONTAL, new Component[] {btnLogin, btnExtrato});
		panel.setLayout(gl_panel);
		
		JPanel extrato = new JPanel();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addComponent(panel, GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
				.addComponent(extrato, GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 151, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(extrato, GroupLayout.PREFERRED_SIZE, 443, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		
		JScrollPane scrollPane = new JScrollPane();
		GroupLayout gl_extrato = new GroupLayout(extrato);
		gl_extrato.setHorizontalGroup(
			gl_extrato.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_extrato.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane)
					.addContainerGap())
		);
		gl_extrato.setVerticalGroup(
			gl_extrato.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_extrato.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane)
					.addContainerGap())
		);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Descri\u00E7\u00E3o", "Valor"
			}
		));
		scrollPane.setViewportView(table);
		extrato.setLayout(gl_extrato);
		contentPane.setLayout(gl_contentPane);
	}
}
